	* ------------------------------------------------------------------------------
	* MASTER SCRIPT:	Replication Code for all Tables and Figures (main.do)
	* Project: School Discipline Disparities Increase when Neighborhood Black Pop Changes
	* Authors: Candipan & Hailey (Science Advances)
	* Last update:	Dec 2025
	* ------------------------------------------------------------------------------
	* PURPOSE: 
	* This script reproduces all descriptive- and regression-based tables (Tables 1; S1-S7) 
	* and figures from the main text and appendix (Figures 1-4; S1-S5)
	*
	* INPUT DATA:  	$data\sa_analysis_final.dta
	* OUTPUT:      	$project\figures
	* OUTPUT:      	$project\tables
	* ------------------------------------------------------------------------------
	* ---------------------------------------------------------
	* DATA NOTES:
	* This script uses the final analysis file: "sa_analysis_final.dta".
	* For details on raw data sources and construction, see README.txt
	* This is the master script which replicates all tables and figures. 
	* If using the tables and figures do files as standalone scripts, 
	* 		you must define all global macros here first.
	* ---------------------------------------------------------	
	* ------------------------------------------------------------------------------
	* INSTRUCTIONS:
	* This script defines all global macros necessary for the dependent do files. 
	* 	1. Set the path to the project root folder in the $project macro (ln 40)
	* 	2. Ensure all other global macros are set.
	* 	3. Set Stata to version 17 or higher. (ln 60)
	* 	4. Ensure that reghdfe is version 5 and up and all user-written packages are installed.
	*		Toggle/untoggle the ado installation code, as necessary. (lns 82-103)
	* 	5. Run the script.
	* ------------------------------------------------------------------------------

		clear all
		set more off

	* --- USER: SET PATH HERE ---
	*  Change this path to the folder where you unzipped the replication package
	*	  Ensure the Stata do files are stored here in root folder
		global project /* set path to project root folder here*/	
	* --- SET DATA PATH ---
		global data   "$project\data"

	* --- DIRECTORIES (Do not edit) ---
		global figures   "$project\figures"
		global tables   "$project\tables"

	* --- AUTO-CREATE OUTPUT FOLDER ---
		capture mkdir "$figures"
		capture mkdir "$tables"


		
	
	************************************************************************
	* SOFTWARE REQUIREMENTS AND SETUP
	************************************************************************

		* Set Stata version 
		version 19 // set to correct version; 
			// this code successfully ran on Stata versions 17-19 (Dec 2025)
 
	
	/********************************************************************
	 Required user-written commands

	 This do-file uses several community-contributed Stata commands that
	 are NOT part of official Stata:

	   - reghdfe (must use version 5 or 6)
	   - ppmlhdfe
	   - estout (for eststo/esttab)
	   - coefplot

	 Please install these packages before running the code (toggle/untoggle below).
	 
	 Once installed, these commands do not need to be re-installed for
	 future sessions.
	********************************************************************/	

	* Install stable version of reghdfe (version 5)
		//cap ado uninstall reghdfe
		//ssc install reghdfe, replace

	// Optional: Install development version (6.x) of reghdfe by also installing ftools
		* Install ftools (remove program if it existed previously)
		//cap ado uninstall ftools
		//net install ftools, from("https://raw.githubusercontent.com/sergiocorreia/ftools/master/src/")

		* More reghdfe installation instructions here: 
		//	https://scorreia.com/software/reghdfe/install.html

	* Install coefplot
		//cap ado uninstall coefplot
		//ssc install coefplot

	* Install estout
		//cap ado uninstall estout
		//ssc install estout

	* Install ppmlhdfe
		//cap ado uninstall ppmlhdfe
		//ssc install ppmlhdfe
	

	* Confirm version of reghdfe package
	which reghdfe // version 6.12.3
	
		/*NOTE! 
			The reghdfe syntax works for versions 5 or later. 
		*/
			

	//**********************************************************************
	// DEFINE GLOBAL MACROS 
	//**********************************************************************

						
		* Conditions for analytic sample
		global conditions pblack!=0 & pblack!=. & pblack<=.99 & samp==1 & nschls==2

		* Neighborhood covariates
		global neighvars s_ppov s_pcol s_pop2

		* School composition covariates 
		global schvars c.pblack##c.bpblack c.phisp##c.bphisp c.pfrpl##c.bpfrpl it_enroll2

		* Regression options (IMPORTANT: must run reghdfe version 5 or 6)
		global regopts absorb(idschool statefips) vce(cluster idsd statefips) noomit /*nocons*/

		* Table output options 
		global meanvars s_pnhb s_pnhw s_phisp s_pop2  ///
						s_pcol  s_ppov ///
						pblack  pwhite  phisp  ///
						pfrpl  it_enroll2
		global propvars predom igeotype south
		global omitref 2010.year 0.typeb 2010.year#0.typeb _cons // for reghdfe output
		global omitref2 2010.year 0.typeb 2010.year#0.typeb  // for ppmlhdfe output
		global tabopts 	b(%6.3f) se(%6.3f)  r2(%6.3f) varwidth(18)  ///
							constant starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) noomit ///
							wide nogaps compress replace

		global tabopts2 	b(%6.3f) se(%6.3f) bic(%16.3f)  aic(%16.3f) varwidth(18)  ///
							constant starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) noomit ///
							 nogaps compress replace		
		
		* Figure output options 
		global ciopts ciopts(recast(rspike)) citop

		global figopts ///
			mlabel(cond(@pval<.001, string(@b, "%9.3f") + "***", ///
			cond(@pval<.01, string(@b, "%9.3f") + "**", ///
			cond(@pval<.05, string(@b, "%9.3f") + "*", ///
			cond(@pval<.10, string(@b, "%9.3f") + "^", ///
			string(@b, "%9.3f")))))) ///
			level(95)		

		* Enrollment filter for sensitivity analysis
		global max maxblack>=20 & maxblack<. & maxwhite>=20  & maxwhite<.

		
		
	//**********************************************************************
	// 	DEFINE PROGRAM TO LABEL VARIABLES
	//**********************************************************************
	
		do "$project\02_labelvars.do"
	
	//**********************************************************************
	// REPLICATE TABLE OUTPUT
	//**********************************************************************

		do "$project\03_maketables.do"
	
	//**********************************************************************
	// REPLICATE FIGURES
	//**********************************************************************	
	
		do "$project\04_makefigures.do"
	